#include <mutex>

#include "edge.h"

namespace Elements
{

Edge::Edge(const Edge& other)
    : Element(other)
    , m_beginStateId(other.m_beginStateId)
    , m_endStateId(other.m_endStateId)
{
}

bool Edge::isSame(Element* const other) const
{
    const auto otherElement = dynamic_cast<Edge const*>(other);
    if (nullptr == otherElement)
        return false;

    return bool(
        m_id == otherElement->m_id
        && m_beginStateId == otherElement->m_beginStateId
        && m_endStateId == otherElement->m_endStateId
    );
}

Common::Json Edge::toJson() const
{
    std::shared_lock locker(m_lock);
    Common::Json result;
    result["id"] = m_id;
    result["elementType"] = unsigned(elementType());
    result["beginStateId"] = m_beginStateId;
    result["endStateId"] = m_endStateId;
    return result;
}

void Edge::fromJson(const Common::Json& json)
{
    std::shared_lock locker(m_lock);
    m_id = json["id"].template get<unsigned>();
    m_beginStateId = json["beginStateId"].template get<unsigned>();
    m_endStateId = json["endStateId"].template get<unsigned>();
}

Element* Edge::clone() const
{
    return new Edge(*this);
}

ElementType Edge::elementType() const
{
    return ElementType::Edge;
}

unsigned Edge::beginStateId() const
{
    std::shared_lock locker(m_lock);
    return m_beginStateId;
}

void Edge::setBeginStateId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_beginStateId = value;
}

unsigned Edge::endStateId() const
{
    std::shared_lock locker(m_lock);
    return m_endStateId;
}

void Edge::setEndStateId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_endStateId = value;
}

} // namespace Elements
